﻿using System;

namespace homework03_2
{
    // 定义形状接口
    public interface IShape
    {
        double CalculateArea();  // 计算面积
        bool IsValid();         // 判断形状是否合法
    }

    // 抽象基类 Shape
    public abstract class Shape : IShape
    {
        public abstract double CalculateArea();
        public abstract bool IsValid();
    }

    // 长方形类
    public class Rectangle : Shape
    {
        public double Length { get; set; }
        public double Width { get; set; }

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override double CalculateArea() => Length * Width;
        public override bool IsValid() => Length > 0 && Width > 0;
    }

    // 正方形类
    public class Square : Shape
    {
        public double Side { get; set; }

        public Square(double side)
        {
            Side = side;
        }

        public override double CalculateArea() => Side * Side;
        public override bool IsValid() => Side > 0;
    }

    // 三角形类
    public class Triangle : Shape
    {
        public double Side1 { get; set; }
        public double Side2 { get; set; }
        public double Side3 { get; set; }

        public Triangle(double side1, double side2, double side3)
        {
            Side1 = side1;
            Side2 = side2;
            Side3 = side3;
        }

        public override double CalculateArea()
        {
            if (!IsValid()) return 0;
            double s = (Side1 + Side2 + Side3) / 2;
            return Math.Sqrt(s * (s - Side1) * (s - Side2) * (s - Side3));
        }

        public override bool IsValid() =>
            Side1 > 0 && Side2 > 0 && Side3 > 0 &&
            (Side1 + Side2 > Side3) && (Side2 + Side3 > Side1) && (Side1 + Side3 > Side2);
    }

    // 测试程序
    class Program2
    {
        static void Main1()
        {
            // 测试长方形
            Rectangle rect = new Rectangle(5, 3);
            Console.WriteLine($"长方形 - 合法: {rect.IsValid()}, 面积: {rect.CalculateArea()}");

            // 测试正方形
            Square square = new Square(4);
            Console.WriteLine($"正方形 - 合法: {square.IsValid()}, 面积: {square.CalculateArea()}");

            // 测试三角形
            Triangle tri = new Triangle(3, 4, 5);
            Console.WriteLine($"三角形 - 合法: {tri.IsValid()}, 面积: {tri.CalculateArea():F2}");

            // 测试非法三角形
            Triangle invalidTri = new Triangle(1, 1, 3);
            Console.WriteLine($"非法三角形 - 合法: {invalidTri.IsValid()}, 面积: {invalidTri.CalculateArea()}");
        }
    }
}
